package com.hm.health.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.hm.health.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liwenlin on 16-11-4.
 */

public class FileListAdapter extends BaseAdapter {

    public static final String GO_ROOT_ITEM_TAG = "goroot";
    public static final String GO_PARENT_ITEM_TAG = "goparent";

    private LayoutInflater mInflater;
    private Bitmap mIcon1;
    private Bitmap mIcon2;
    private Bitmap mIcon3;
    private Bitmap mIcon4;
    private List<String> mListItems;
    private List<String> mSelectedPath;
    private Map<Integer, Boolean> mIsSelectedMap;
    private Map<Integer, Integer> mIsVisibilityMap;
    public Map<Integer, Integer> getIsVisibilityMap() {
        return mIsVisibilityMap;
    }

    private List<ViewHolder> mViewHolder = new ArrayList<ViewHolder>();
    private Context mContext;

    public Map<Integer, Boolean> getIsSelectedMap() {
        return mIsSelectedMap;
    }

    public void setIsSelectedMap(Map<Integer, Boolean> mIsSelectedMap) {
        this.mIsSelectedMap = mIsSelectedMap;
    }

    public FileListAdapter(Context context,List<String> it,List<String> pa) {

        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        if ( it != null && it.size() > 0) {
            mIsSelectedMap = new HashMap<Integer, Boolean>();
            mIsVisibilityMap = new HashMap<Integer, Integer>();
            for (int i = 0; i < it.size(); i++) {
                mIsSelectedMap.put(i, false);
                mIsVisibilityMap.put(i, View.GONE);
            }
        }
        mListItems = it;
        mSelectedPath = pa;
        mIcon1 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_folder);
        mIcon2 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_folder);
        mIcon3 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.ic_folder);
        mIcon4 = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.doc);
    }

    @Override
    public int getCount() {
        return mListItems.size();
    }

    @Override
    public Object getItem(int position) {
        return mListItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.file_list_item, null);
            holder = new ViewHolder();
            mViewHolder.add(holder);
            holder.text = convertView.findViewById(R.id.filetext);
            holder.icon = convertView.findViewById(R.id.fileicon);
            holder.cBox = convertView.findViewById(R.id.file_check);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        File f = new File(mSelectedPath.get(position).toString());
        if(mListItems.get(position).toString().equals(GO_ROOT_ITEM_TAG)){
            holder.text.setText(mContext.getString(R.string.return_to_root));
            holder.icon.setImageBitmap(mIcon1);
            holder.cBox.setChecked(mIsSelectedMap.get(position));
            int status = mIsVisibilityMap.get(position);
            if (status == View.VISIBLE) {
                status = View.VISIBLE;
            } else if (status == View.INVISIBLE) {
                status = View.INVISIBLE;
            } else {
                status = View.GONE;
            }
            holder.cBox.setVisibility(status);
        } else if (mListItems.get(position).toString().equals(GO_PARENT_ITEM_TAG)) {
            holder.text.setText(mContext.getString(R.string.return_to_parent));
            holder.icon.setImageBitmap(mIcon2);
            holder.cBox.setChecked(mIsSelectedMap.get(position));
            int status = mIsVisibilityMap.get(position);
            if (status == View.VISIBLE) {
                status = View.VISIBLE;
            } else if (status == View.INVISIBLE) {
                status = View.INVISIBLE;
            } else {
                status = View.GONE;
            }
            holder.cBox.setVisibility(status);
        }else{
            holder.text.setText(f.getName());
            if (f.isDirectory()) {
                holder.icon.setImageBitmap(mIcon3);
                holder.cBox.setChecked(mIsSelectedMap.get(position));
                int status = mIsVisibilityMap.get(position);
                if (status == View.VISIBLE) {
                    status = View.VISIBLE;
                } else if (status == View.INVISIBLE) {
                    status = View.INVISIBLE;
                } else {
                    status = View.GONE;
                }
                holder.cBox.setVisibility(status);
            } else {
                holder.icon.setImageBitmap(mIcon4);
                holder.cBox.setChecked(mIsSelectedMap.get(position));
                int status = mIsVisibilityMap.get(position);
                if (status == View.VISIBLE) {
                    status = View.VISIBLE;
                } else if (status == View.INVISIBLE) {
                    status = View.INVISIBLE;
                } else {
                    status = View.GONE;
                }
                holder.cBox.setVisibility(status);
            }
        }
        return convertView;
    }

    public List<ViewHolder> getViewHolder() {
        return mViewHolder;
    }

    public final class ViewHolder
    {
        public TextView text;
        public ImageView icon;
        public CheckBox cBox;
    }
}
